//
//  RechargeViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/11.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "RechargeViewController.h"
#import "FinancialRecordViewController.h"

#import <AlipaySDK/AlipaySDK.h>
#import "APOrderInfo.h"
//#import "APRSASigner.h"


@interface RechargeViewController ()

@property (strong, nonatomic) UIImageView *alipayBg;
@property (strong, nonatomic) UIImageView *arrowBg;
@property (strong, nonatomic) UIImageView *quBg;

@property (strong, nonatomic) UILabel *alipayL;
@property (strong, nonatomic) UIImageView *alipaySmallIV;
//@property (strong, nonatomic) UILabel *alipayCountL;
//@property (strong, nonatomic) UIView *alipayLineIV;

//@property (strong, nonatomic) UILabel *phoneL;
//@property (strong, nonatomic) UILabel *phoneNumL;
//@property (strong, nonatomic) UIView *phoneLineIV;

//@property (strong, nonatomic) UILabel *codeL;
//@property (strong, nonatomic) UITextField *codeTextField;
//@property (strong, nonatomic) UIButton *getCodeBtn;
//@property (strong, nonatomic) UIView *codeLineIV;

@property (strong, nonatomic) UILabel *rechargeMoneyL;
@property (strong, nonatomic) UITextField *rechargeMomeyNumL;
@property (strong, nonatomic) UIView *rechargeLineIV;

@property (strong, nonatomic) UIImageView *rechargeBG;
@property (strong, nonatomic) UILabel *rechargeL;

@end

@implementation RechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WhiteColor;
    self.title = @"充值";

    UIButton *rigthBtn = [[UIButton alloc] init];
    rigthBtn.frame = CGRectMake(0, 0, jisuan(110), jisuan(50));
    [rigthBtn setTitle:@"充值记录" forState:UIControlStateNormal];
    [rigthBtn setTitle:@"充值记录" forState:UIControlStateSelected];
    [rigthBtn setTitleColor:Color333333 forState:UIControlStateNormal];
     [rigthBtn setTitleColor:Color333333 forState:UIControlStateSelected];
    rigthBtn.titleLabel.font = Font(24);
    [rigthBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rigthBtn];
    self.navigationItem.rightBarButtonItem = rightItem;

    [self createView];
}

#pragma mark -确认充值
//- (void)rechargeClick:(UIButton *)btn

- (void)rechargeClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"确认充值点击");
    
    if (self.rechargeMomeyNumL.text.length <= 0) {
        
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请输入与充值金额"];
        return ;
    }
   
    NSString *userid = [RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey];
    NSString *money = [RSA encryptString:self.rechargeMomeyNumL.text publicKey:PublicKey];
    NSString *paytype = [RSA encryptString:@"1" publicKey:PublicKey];
    
    NSString *str = [NSString stringWithFormat:@"{\"userid\" : \"%@\", \"money\" : \"%@\", \"paytype\" : \"%@\"}", userid, money, paytype];
    
    [PPNetworkHelper POST:@"pay/Recharge/recharge" parameters:@{@"json" : str} success:^(id responseObject) {
        
        NSLog(@"充值接口：%@", responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            [[CommonAction sharedInstance] goAlipayWithRsa2PrivateKey:responseObject[@"data"]];
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
        
    }];
    
}




#pragma mark -获取验证码
//- (void)getCodeClick:(UIButton *)btn {
//
//    NSLog(@"获取验证码");
//
//}

#pragma mark -充值记录
- (void)rightBtnClick:(UIButton *)btn {
    
    NSLog(@"充值记录点击");
    
    FinancialRecordViewController *vc = [[FinancialRecordViewController alloc] init];
    
    vc.type = 2;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark -createView
- (void)createView {

    [self.view addSubview:self.alipayBg];
    [self.view addSubview:self.arrowBg];
    [self.view addSubview:self.quBg];
    
    [self.view addSubview:self.alipayL];
    [self.view addSubview:self.alipaySmallIV];
//    [self.view addSubview:self.alipayCountL];
//    [self.view addSubview:self.alipayLineIV];
    
//    [self.view addSubview:self.phoneL];
//    [self.view addSubview:self.phoneNumL];
//    [self.view addSubview:self.phoneLineIV];
    
//    [self.view addSubview:self.codeL];
//    [self.view addSubview:self.codeTextField];
//    [self.view addSubview:self.getCodeBtn];
//    [self.view addSubview:self.codeLineIV];
    
    [self.view addSubview:self.rechargeMoneyL];
    [self.view addSubview:self.rechargeMomeyNumL];
    [self.view addSubview:self.rechargeLineIV];
    
    [self.view addSubview:self.rechargeBG];
    [self.view addSubview:self.rechargeL];
    
    
    __weak typeof(self) weakSelf = self;
    
    [self.arrowBg mas_makeConstraints:^(MASConstraintMaker *make) {
        
//        make.left.mas_equalTo(weakSelf.alipayBg.mas_right).with.offset(jisuan(34));
      
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.top.mas_equalTo(weakSelf.view.mas_top).with.offset(jisuan(102));
        make.width.mas_equalTo(jisuan(92));
        make.height.mas_equalTo(jisuan(28));
    }];
    [self.alipayBg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.arrowBg.mas_left).with.offset(-jisuan(34));
        make.top.mas_equalTo(weakSelf.view.mas_top).with.offset(jisuan(70));
        make.width.mas_equalTo(jisuan(100));
        make.height.mas_equalTo(jisuan(100));
    }];
    [self.quBg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.arrowBg.mas_right).with.offset(jisuan(34));
        make.top.mas_equalTo(weakSelf.view.mas_top).with.offset(jisuan(70));
        make.width.mas_equalTo(jisuan(100));
        make.height.mas_equalTo(jisuan(100));
    }];
    
    [self.alipaySmallIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.top.mas_equalTo(weakSelf.alipayBg.mas_bottom).with.offset(jisuan(100));
        make.width.mas_equalTo(jisuan(50));
        make.height.mas_equalTo(jisuan(50));
    }];
    
    
    [self.alipayL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.alipaySmallIV.mas_right).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.centerY.mas_equalTo(weakSelf.alipaySmallIV.mas_centerY);
        make.height.mas_equalTo(jisuan(100));
    }];

//    [self.alipayLineIV mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
//        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
//        make.top.mas_equalTo(weakSelf.alipayL.mas_bottom).with.offset(jisuan(28));
//        make.height.mas_equalTo(jisuan(2));
//
//    }];
    
//    [self.phoneL mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
//        make.top.mas_equalTo(weakSelf.alipayLineIV.mas_bottom).with.offset(jisuan(26));
//        make.width.mas_equalTo(jisuan(86));
//        make.height.mas_equalTo(jisuan(34));
//    }];
//    [self.phoneNumL mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerY.mas_equalTo(weakSelf.phoneL.mas_centerY);
//        make.left.mas_equalTo(weakSelf.phoneL.mas_right).with.offset(jisuan(64));
//        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
//        //        make.width.mas_equalTo(jisuan(160));
//        make.height.mas_equalTo(jisuan(34));
//    }];
//    [self.phoneLineIV mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
//        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
//        make.top.mas_equalTo(weakSelf.phoneL.mas_bottom).with.offset(jisuan(28));
//        make.height.mas_equalTo(jisuan(2));
//
//    }];
    
//    [self.codeL mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
//        make.top.mas_equalTo(weakSelf.phoneLineIV.mas_bottom).with.offset(jisuan(26));
//        make.width.mas_equalTo(jisuan(86));
//        make.height.mas_equalTo(jisuan(34));
//    }];
//    [self.getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(64));
//        make.centerY.mas_equalTo(weakSelf.codeL.mas_centerY);
//        make.width.mas_equalTo(jisuan(130));
//        make.height.mas_equalTo(jisuan(34));
//    }];
    
//    [self.codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(weakSelf.codeL.mas_right).with.offset(jisuan(64));
//        make.centerY.mas_equalTo(weakSelf.codeL.mas_centerY);
////        make.width.mas_equalTo(jisuan(120));
//        make.right.mas_equalTo(weakSelf.getCodeBtn.mas_left).with.offset(-jisuan(40));
//        make.height.mas_equalTo(jisuan(34));
//    }];
//    [self.codeLineIV mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
//        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
//        make.top.mas_equalTo(weakSelf.codeL.mas_bottom).with.offset(jisuan(28));
//        make.height.mas_equalTo(jisuan(2));
//    }];
    
    [self.rechargeMoneyL mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
    make.top.mas_equalTo(weakSelf.alipaySmallIV.mas_bottom).with.offset(jisuan(26));
        
        make.width.mas_equalTo(jisuan(106));
        make.height.mas_equalTo(jisuan(34));

    }];
    
    [self.rechargeMomeyNumL mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(weakSelf.rechargeMoneyL.mas_right).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.centerY.mas_equalTo(weakSelf.rechargeMoneyL.mas_centerY);
        make.height.mas_equalTo(jisuan(34));

    }];

    [self.rechargeLineIV mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(40));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(40));
        make.top.mas_equalTo(weakSelf.rechargeMoneyL.mas_bottom).with.offset(jisuan(28));
        make.height.mas_equalTo(jisuan(2));

    }];
    [self.rechargeBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).with.offset(-jisuan(66));
        make.width.mas_equalTo(jisuan(550));
        make.height.mas_equalTo(jisuan(106));

    }];
    [self.rechargeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.rechargeBG.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.rechargeBG.mas_centerY).with.offset(-jisuan(4));


    }];

}

#pragma mark -lazy
- (UIImageView *)alipayBg {
    
    if (!_alipayBg) {
        
        _alipayBg = [[UIImageView alloc] init];
        _alipayBg.contentMode = UIViewContentModeScaleAspectFit;
        _alipayBg.image = [UIImage imageNamed:@"alipayBG"];
    }
    
    return _alipayBg;
}
- (UIImageView *)arrowBg {
    
    if (!_arrowBg) {
        
        _arrowBg = [[UIImageView alloc] init];
        _arrowBg.contentMode = UIViewContentModeScaleAspectFit;
        _arrowBg.image = [UIImage imageNamed:@"arrowBG"];
    }
    
    return _arrowBg;
}
- (UIImageView *)quBg {
    
    if (!_quBg) {
        
        _quBg = [[UIImageView alloc] init];
        _quBg.contentMode = UIViewContentModeScaleAspectFit;
        _quBg.image = [UIImage imageNamed:@"rechargeLogoBG"];
    }
    
    return _quBg;
}

- (UIImageView *)alipaySmallIV {
    
    if (!_alipaySmallIV) {
        
        _alipaySmallIV = [[UIImageView alloc] init];
        _alipaySmallIV.image = [UIImage imageNamed:@"alipayBG"];
        _alipaySmallIV.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    return _alipaySmallIV;
}

- (UILabel *)alipayL {

    if (!_alipayL) {

        _alipayL = [[UILabel alloc] init];
        _alipayL.text = @"支付宝";
        _alipayL.textColor = Color333333;
        _alipayL.font = Font(24);
        _alipayL.textAlignment = NSTextAlignmentLeft;

    }

    return _alipayL;
}

//- (UILabel *)alipayCountL {
//
//    if (!_alipayCountL) {
//
//        _alipayCountL = [[UILabel alloc] init];
//        _alipayCountL.text = @"15045483071";
//        _alipayCountL.textColor = Color333333;
//        _alipayCountL.font = Font(24);
//        _alipayCountL.textAlignment = NSTextAlignmentLeft;
//
//    }
//
//    return _alipayCountL;
//}

//- (UIView *)alipayLineIV {
//
//    if (!_alipayLineIV) {
//
//        _alipayLineIV = [[UIView alloc] init];
//        _alipayLineIV.backgroundColor = Colordddddd;
//    }
//
//    return _alipayLineIV;
//}

//- (UILabel *)phoneL {
//
//    if (!_phoneL) {
//
//        _phoneL = [[UILabel alloc] init];
//        _phoneL.text = @"手机号";
//        _phoneL.textColor = Color333333;
//        _phoneL.font = Font(24);
//        _phoneL.textAlignment = NSTextAlignmentLeft;
//
//    }
//
//    return _phoneL;
//}

//- (UILabel *)phoneNumL {
//
//    if (!_phoneNumL) {
//
//        _phoneNumL = [[UILabel alloc] init];
//        _phoneNumL.text = @"150****3071";
//        _phoneNumL.textColor = Color333333;
//        _phoneNumL.font = Font(24);
//        _phoneNumL.textAlignment = NSTextAlignmentLeft;
//
//    }
//
//    return _phoneNumL;
//}

//- (UIView *)phoneLineIV {
//
//    if (!_phoneLineIV) {
//
//        _phoneLineIV = [[UIView alloc] init];
//        _phoneLineIV.backgroundColor = Colordddddd;
//    }
//
//    return _phoneLineIV;
//}

//- (UILabel *)codeL {
//
//    if (!_codeL) {
//
//        _codeL = [[UILabel alloc] init];
//        _codeL.text = @"验证码";
//        _codeL.textColor = Color333333;
//        _codeL.font = Font(24);
//        _codeL.textAlignment = NSTextAlignmentLeft;
//
//    }
//
//    return _codeL;
//
//}

//- (UITextField *)codeTextField {
//
//    if (!_codeTextField) {
//
//        _codeTextField = [[UITextField alloc] init];
//        _codeTextField.textAlignment = NSTextAlignmentLeft;
//        _codeTextField.textColor = Color333333;
//        _codeTextField.placeholder = @"请输入验证码";
//        _codeTextField.font = Font(24);
//        _codeTextField.keyboardType = UIKeyboardTypeNumberPad;
//
//    }
//
//    return _codeTextField;
//}

//- (UIButton *)getCodeBtn {
//
//    if (!_getCodeBtn) {
//
//        _getCodeBtn = [[UIButton alloc] init];
//        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
//
//        [_getCodeBtn setTitleColor:Colorff4200 forState:UIControlStateNormal];
//        _getCodeBtn.titleLabel.font = Font(24);
//
//        [_getCodeBtn addTarget:self action:@selector(getCodeClick:) forControlEvents:UIControlEventTouchUpInside];
//
//    }
//
//    return _getCodeBtn;
//}

//- (UIView *)codeLineIV {
//
//    if (!_codeLineIV) {
//
//        _codeLineIV = [[UIView alloc] init];
//        _codeLineIV.backgroundColor = Colordddddd;
//    }
//
//    return _codeLineIV;
//}

- (UILabel *)rechargeMoneyL {
    
    if (!_rechargeMoneyL) {
    
        _rechargeMoneyL = [[UILabel alloc] init];
        _rechargeMoneyL.text = @"充值金额";
        _rechargeMoneyL.textColor = Color333333;
        _rechargeMoneyL.font = Font(24);
        _rechargeMoneyL.textAlignment = NSTextAlignmentLeft;
    }

    return _rechargeMoneyL;
}

- (UITextField *)rechargeMomeyNumL {
    
    if (!_rechargeMomeyNumL) {
    
        _rechargeMomeyNumL = [[UITextField alloc] init];
        _rechargeMomeyNumL.textAlignment = NSTextAlignmentLeft;
        _rechargeMomeyNumL.placeholder = @"请输入充值金额";
        _rechargeMomeyNumL.textColor = Color333333;
        _rechargeMomeyNumL.font = Font(24);
        _rechargeMomeyNumL.keyboardType = UIKeyboardTypeDecimalPad;
        
    }
    
    return _rechargeMomeyNumL;
    
}

- (UIView *)rechargeLineIV {
    
    if (!_rechargeLineIV) {
        
        _rechargeLineIV = [[UIView alloc] init];
        _rechargeLineIV.backgroundColor = Colordddddd;
    }
    
    return _rechargeLineIV;
    
}
- (UIImageView *)rechargeBG {
    
    if (!_rechargeBG) {
        
        _rechargeBG = [[UIImageView alloc] init];
        _rechargeBG.contentMode = UIViewContentModeScaleAspectFit;
        _rechargeBG.image = [UIImage imageNamed:@"redBtnBG"];
    
        
        _rechargeBG.userInteractionEnabled = YES;
        UITapGestureRecognizer *rechargeGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rechargeClick:)];
        [_rechargeBG addGestureRecognizer:rechargeGesture];
    }
    
    return _rechargeBG;
}

- (UILabel *)rechargeL {
    
    if (!_rechargeL) {
        
        _rechargeL = [[UILabel alloc] init];
        _rechargeL.textAlignment = NSTextAlignmentCenter;
        _rechargeL.text = @"确认充值";
        _rechargeL.textColor = WhiteColor;
        _rechargeL.font = Font(32);
        
//        _rechargeL.userInteractionEnabled = YES;
//        UITapGestureRecognizer *rechargeGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rechargeClick:)];
//        [_rechargeL addGestureRecognizer:rechargeGesture];
    }
    
    return _rechargeL;
    
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
//    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
